package com.zhijian.wharf.service.impl;

import com.zhijian.wharf.dao.DatabaseBackupsMapper;
import com.zhijian.wharf.entity.po.DatabaseBackups;
import com.zhijian.wharf.entity.vo.request.DatabaseBackupsSearchVo;
import com.zhijian.wharf.entity.vo.request.DatabaseBackupsSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.response.DatabaseBackupsVo;
import com.zhijian.wharf.service.DatabaseBackupsService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 数据库备份记录业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-08-16 16:13:03
 */
@Slf4j
@Service
public class DatabaseBackupsServiceImpl implements DatabaseBackupsService {

	@Autowired
    private DatabaseBackupsMapper mapper;

    @Override
    public DatabaseBackups selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DatabaseBackupsVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(DatabaseBackups databaseBackups) {
        Date now = new Date();
        databaseBackups.setCreateTime(now);
        databaseBackups.setUpdateTime(now);
        return mapper.insert(databaseBackups) > 0 ? databaseBackups.getId() : 0L;
    }

    @Override
    public Long insertSelective(DatabaseBackups databaseBackups) {
        Date now = new Date();
        databaseBackups.setCreateTime(now);
        databaseBackups.setUpdateTime(now);
        return mapper.insertSelective(databaseBackups) > 0 ? databaseBackups.getId() : 0L;
    }

    @Override
    public boolean update(DatabaseBackups databaseBackups) {
        Date now = new Date();
        databaseBackups.setUpdateTime(now);
        return mapper.updateByPrimaryKey(databaseBackups) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(DatabaseBackups record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(DatabaseBackupsSaveOrUpdateVo saveOrUpdateVo) {
        DatabaseBackups databaseBackups = new DatabaseBackups();
        BeanUtils.copyProperties(saveOrUpdateVo, databaseBackups);
        return this.insert(databaseBackups) != 0;
    }

    @Override
    public boolean modify(DatabaseBackupsSaveOrUpdateVo saveOrUpdateVo) {
        DatabaseBackups old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        DatabaseBackups databaseBackups = new DatabaseBackups();
        BeanUtils.copyProperties(saveOrUpdateVo, databaseBackups);
        return this.update(databaseBackups);
    }

    @Override
    public DatabaseBackupsVo selectOne(DatabaseBackupsSearchVo databaseBackupsSearchVo) {
        return mapper.selectOne(databaseBackupsSearchVo);
     }

    @Override
    public Integer count(DatabaseBackupsSearchVo databaseBackupsSearchVo) {
        return mapper.count(databaseBackupsSearchVo);
    }

    @Override
    public List<DatabaseBackupsVo> selectList(DatabaseBackupsSearchVo databaseBackupsSearchVo) {
        return mapper.selectList(databaseBackupsSearchVo);
    }

    @Override
    public Page<DatabaseBackupsVo> selectPage(DatabaseBackupsSearchVo databaseBackupsSearchVo) {
        AssertUtil.check(Objects.isNull(databaseBackupsSearchVo.getPageNum()) || Objects.isNull(databaseBackupsSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(databaseBackupsSearchVo);
        //分页
        Page<DatabaseBackupsVo> page = new Page<>(true, count, databaseBackupsSearchVo.getPageNum(), databaseBackupsSearchVo.getPageSize());
        databaseBackupsSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DatabaseBackupsVo> responseVos = this.selectList(databaseBackupsSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<DatabaseBackups> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(DatabaseBackupsSearchVo databaseBackupsSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(databaseBackupsSearchVo);
    }
    
}